package top.kaoshanji.p2ch19a.support;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/28 10:12
 */
public class Tourist extends Thread {

    private CyclicBarrier barrier;

    public Tourist(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    @Override
    public void run() {

        try {
            // 模拟先各自独立运行
            Thread.sleep((int) (Math.random() * 1000));

            // 集合点A
            barrier.await();
            System.out.println(this.getName() + " arrived A " + System.currentTimeMillis());

            // 集合后模拟再各自独立运行
            Thread.sleep((int) (Math.random() * 1000));

            // 集合点B
            barrier.await();
            System.out.println(this.getName() + " arrived B " + System.currentTimeMillis());

        } catch (InterruptedException e) {
            /// ///
        } catch (BrokenBarrierException e) {
            /// ////
        }


    }
}
